package org.pps.gl.client.vue;

import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.LoginInfo;
import org.pps.gl.client.Pps_gl;
import org.pps.gl.client.data.UserInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.datepicker.client.DateBox;

/**
 * Première page du profil utilisateur en visualisation
 * 
 */
public class PLF_V_Identite_ReadOnly extends Composite {

	private Label lblNom, lblDateNaissance, lblStatut, lblSexe;
	@SuppressWarnings("unused")
	private TextBox txtBoxNom, txtBoxPrenom, txtBoxStatut;
	private VerticalPanel verticalPanel;
	private HorizontalPanel HPButton;
	private DockPanel dockPanel;

	private Image imgPhotoProfil;
	@SuppressWarnings("unused")
	private ListBox listBoxSexe;
	@SuppressWarnings("unused")
	private DateBox dateBox;
	@SuppressWarnings("unused")
	private String email, sexe;

	UserInfo info;

	private Button add;

	public PLF_V_Identite_ReadOnly(String email) {

		dockPanel = new DockPanel();
		initWidget(dockPanel);
		imgPhotoProfil = new Image("images/logos/p_homme.png");
		verticalPanel = new VerticalPanel();
		lblNom = new Label();
		lblDateNaissance = new Label();

		HPButton = new HorizontalPanel();
		lblStatut = new Label();
		lblSexe = new Label("");
		dateBox = new DateBox();
		this.email = email;
		add = new Button();
		
		setInfo();
		
		
	}

	protected void afficherProfil() {

		dockPanel.clear();

		dockPanel.setStyleName("profilMarge");
		dockPanel.setSize("450px", "235px");

		dockPanel.add(imgPhotoProfil, DockPanel.WEST);
		dockPanel.setCellHorizontalAlignment(imgPhotoProfil,
				HasHorizontalAlignment.ALIGN_CENTER);
		dockPanel.setCellVerticalAlignment(imgPhotoProfil,
				HasVerticalAlignment.ALIGN_MIDDLE);

		dockPanel.add(verticalPanel, DockPanel.CENTER);
		dockPanel.setCellVerticalAlignment(verticalPanel,
				HasVerticalAlignment.ALIGN_BOTTOM);

		verticalPanel.add(lblNom);
		verticalPanel.setCellVerticalAlignment(lblNom,
				HasVerticalAlignment.ALIGN_BOTTOM);

		verticalPanel.add(lblSexe);
		verticalPanel.setCellVerticalAlignment(lblSexe,
				HasVerticalAlignment.ALIGN_BOTTOM);

		verticalPanel.add(lblDateNaissance);
		verticalPanel.setCellVerticalAlignment(lblDateNaissance,
				HasVerticalAlignment.ALIGN_BOTTOM);
		
		verticalPanel.add(add);

		dockPanel.add(HPButton, DockPanel.EAST);
		dockPanel.setCellHorizontalAlignment(HPButton,
				HasHorizontalAlignment.ALIGN_CENTER);
		dockPanel.setCellVerticalAlignment(HPButton,
				HasVerticalAlignment.ALIGN_MIDDLE);

		lblStatut.setStyleName("dynTitre2");

		dockPanel.add(lblStatut, DockPanel.SOUTH);
		dockPanel.setCellVerticalAlignment(lblStatut,
				HasVerticalAlignment.ALIGN_MIDDLE);
	}

	/**
	 * Méthode qui remplit les informations de l'utilisateur.
	 */
	public void setInfo() {

		GetInfoServiceAsync gi = GWT.create(GetInfoService.class);

		gi.getUserIdentite(email, new AsyncCallback<UserInfo>() {

			@Override
			public void onSuccess(final UserInfo result) {
				info = result;
				
				String s= info.getSexe();
				
				if (info.getSexe().equals("M"))
					imgPhotoProfil = new Image("images/logos/p_homme.png");
				else
					imgPhotoProfil = new Image("images/logos/p_femme.png");
				
				lblNom.setText(info.getFirstname() + " " + info.getLastname());
				lblDateNaissance.setText(info.getDateNaissance());
				lblStatut.setText(info.getStatut());
				if (!result.getEmailAddress().equals(
						LoginInfo.getCurrentUser().getEmailAddress())) {
					// Déjà contact
					if (LoginInfo.getCurrentUser().getUserInfo().getContacts()
							.contains(result.getEmailAddress())) {
						add.setText("Déjà contact");
						add.setEnabled(false);
					} // Déjà demandé par l'autre--> confirmer
					else if (LoginInfo.getCurrentUser().getReceived()
							.contains(result.getEmailAddress())) {
						add.setText("Accepter la demande");
						// Add clickhandler confirmer demande
						add.addClickHandler(new ClickHandler() {
							@Override
							public void onClick(ClickEvent event) {
								GetInfoServiceAsync gi = GWT
										.create(GetInfoService.class);
								gi.confirmContactDemande(result
										.getEmailAddress(), LoginInfo
										.getCurrentUser().getEmailAddress(),
										new AsyncCallback<Boolean>() {
											@Override
											public void onFailure(
													Throwable caught) {
												add.setText("Erreur Connection");
											}

											@Override
											public void onSuccess(Boolean result) {
												if (result) {
													add.setText("Ajouté");
													add.setEnabled(false);
													Pps_gl.getCm().refresh();
												} else
													add.setText("Erreur Serveur");
											}
										});

							}

						});
					}
					// déjà demandé par lui
					else if (LoginInfo.getCurrentUser().getDemandes()
							.contains(result.getEmailAddress())) {
						add.setText("Déjà demandé");
						add.setEnabled(false);
					} // Demander contact
					else {
						add.setText("Ajouter " + result.getFirstname());
						add.addClickHandler(new ClickHandler() {
							@Override
							public void onClick(ClickEvent event) {
								GetInfoServiceAsync gi = GWT
										.create(GetInfoService.class);
								gi.createContactDemande(LoginInfo
										.getCurrentUser().getEmailAddress(),
										result.getEmailAddress(),
										new AsyncCallback<Boolean>() {
											@Override
											public void onFailure(
													Throwable caught) {
												add.setText("Erreur Serveur");
											}

											@Override
											public void onSuccess(Boolean result) {
												if (result) {
													add.setText("Demandé!");
													add.setEnabled(false);
													Pps_gl.getCm().refresh();
												} else
													add.setText("Erreur");
											}
										});

							}

						});
					}
				} else {
					add.setText("Moi");
					add.setEnabled(false);
				}

				if (info.getSexe().equals("M"))
					lblSexe.setText("Homme");
				else
					lblSexe.setText("Femme");
				
				afficherProfil();
			}

			@Override
			public void onFailure(Throwable caught) {
				afficherProfil();

			}
		});

	}

}
